import { axios } from "@/services/axios";
import {useEffect, useMemo, useState} from "react";

const Workflow_state = {
  getlist: params => axios.post(`api/workflow_state/list`, params),
  getuserlist: params => axios.post(`api/workflow_user/list`, params),
  getcollist: params => axios.post(`api/workflow_col/list`, params),
  get: data => axios.get(`api/workflow_state/${data.id}`),
  create: data => axios.post("api/workflow_state", data),
  save: data => axios.post(`api/workflow_state/${data.id}`, data),
  delete: data => axios.post(`api/workflow_state/delete/${data.id}`),
};
export function useStateItems(enumcode) {
  const [stateLoaded, setstateLoaded] = useState(false);
  const [statedata, setstatedata] = useState([]);

  useEffect(() => {
    let cancelDataSourceLoading = false;
    let params={
      page: 1,
      pagesize: 100,
      name: "",
      workflowid: enumcode,
    }
    Workflow_state.getlist(params).then(data => {
      if (!cancelDataSourceLoading) {
        setstateLoaded(true);
        setstatedata(data.data);
    }});

    return () => {
      cancelDataSourceLoading = true;
    };
  }, [enumcode]);

  return useMemo(() => ({ stateLoaded, statedata }), [stateLoaded, statedata]);
};
export function useUserItems(enumcode) {
  const [userLoaded, setuserLoaded] = useState(false);
  const [userdata, setuserdata] = useState([]);

  useEffect(() => {
    let cancelDataSourceLoading = false;
    let params={
      page: 1,
      pagesize: 500,
      name: "",
    }
    Workflow_state.getuserlist(params).then(data => {
      if (!cancelDataSourceLoading) {
        setuserLoaded(true);
        setuserdata(data.data);
    }});

    return () => {
      cancelDataSourceLoading = true;
    };
  }, [enumcode]);

  return useMemo(() => ({ userLoaded, userdata }), [userLoaded, userdata]);
};
export function useColItems(enumcode) {
  const [colLoaded, setcolLoaded] = useState(false);
  const [coldata, setcoldata] = useState([]);
  useEffect(() => {
    let cancelDataSourceLoading = false;
    let params={
      page: 1,
      pagesize: 100,
      tablename: enumcode,
    }
    Workflow_state.getcollist(params).then(data => {
      if (!cancelDataSourceLoading) {
        setcolLoaded(true);
        setcoldata(data.data);
    }});

    return () => {
      cancelDataSourceLoading = true;
    };
  }, [enumcode]);

  return useMemo(() => ({ colLoaded, coldata }), [colLoaded, coldata]);
};
export default Workflow_state;


